<template>
  <div class="reportFormPage">
    <TopOperation ref="TopOperation" :tabs-list="tabList" :active-val.sync="activeVal" @currentActive="currentActive"
      @reportFormsSubmit="reportFormsSubmit" @printSpecificPart="onPrintSpecificPart" />
    <div ref="printView" v-loading="modPagesLodding">
      <div v-show="activeVal === 'tdhslb'" v-for="(item, index) in excelTypeMap['tdhslb']" :key="'tdhslb' + index">
        <tdhslb ref="tdhslb" :excel-id="item" :page="index" :syType="form.syType"></tdhslb>
      </div>
      <div v-show="activeVal === 'tdmdjlb'" v-for="(item, index) in excelTypeMap['tdmdjlb']" :key="'tdmdjlb' + index">
        <tdmdjlb ref="tdmdjlb" :excel-id="item" :page="index" :syType="form.syType"></tdmdjlb>
      </div>
      <div v-show="activeVal === 'tdjxhsljlb'" v-for="(item, index) in excelTypeMap['tdjxhsljlb']"
        :key="'tdjxhsljlb' + index">
        <tdjxhsljlb ref="tdjxhsljlb" :excel-id="item" :page="index" :syType="form.syType"
          @tg345ResultChange="tg345ResultChange"></tdjxhsljlb>
      </div>
      <div v-show="activeVal === 'tdtrcdjlb'" v-for="(item, index) in excelTypeMap['tdtrcdjlb']"
        :key="'tdtrcdjlb' + index">
        <tdtrcdjlb ref="tdtrcdjlb" :excel-id="item" :page="index" :syType="form.syType"></tdtrcdjlb>
      </div>

      <div v-show="activeVal === 'tdjsjlb'" v-for="(item, index) in excelTypeMap['tdjsjlb']" :key="'tdjsjlb' + index">
        <tdjsjlb ref="tdjsjlb" :excel-id="item" :page="index" :syType="form.syType"></tdjsjlb>
      </div>

      <div v-show="activeVal === 'tdczbjlby'" v-for="(item, index) in excelTypeMap['tdczbjlby']"
        :key="'tdczbjlby' + index">
        <tdczbjlby ref="tdczbjlby" :excel-id="item" :page="index" :syType="form.syType"></tdczbjlby>
      </div>

      <div v-show="activeVal === 'tdczbjlbe'" v-for="(item, index) in excelTypeMap['tdczbjlbe']"
        :key="'tdczbjlbe' + index">
        <tdczbjlbe ref="tdczbjlbe" :excel-id="item" :page="index" :syType="form.syType"></tdczbjlbe>
      </div>

      <div v-show="activeVal === 'tdczbjlbs'" v-for="(item, index) in excelTypeMap['tdczbjlbs']"
        :key="'tdczbjlbs' + index">
        <tdczbjlbs ref="tdczbjlbs" :excel-id="item" :page="index" :syType="form.syType"></tdczbjlbs>
      </div>

      <div v-show="activeVal === 'tdsslyjzjlb'" v-for="(item, index) in excelTypeMap['tdsslyjzjlb']"
        :key="'tdsslyjzjlb' + index">
        <tdsslyjzjlb ref="tdsslyjzjlb" :excel-id="item" :page="index" :syType="form.syType"></tdsslyjzjlb>
      </div>

      <div v-show="activeVal === 'tdklfxjlb'" v-for="(item, index) in excelTypeMap['tdklfxjlb']"
        :key="'tdklfxjlb' + index">
        <tdklfxjlb ref="tdklfxjlb" :excel-id="item" :page="index" :syType="form.syType"></tdklfxjlb>
      </div>

      <div v-show="activeVal === 'tgsybg'" v-for="(item, index) in excelTypeMap['tgsybg']" :key="'tgsybg' + index">
        <tgsybg ref="tgsybg" :excel-id="item" :page="index" :syType="form.syType"></tgsybg>
      </div>

      <div v-show="activeVal === 'tjssybg'" v-for="(item, index) in excelTypeMap['tjssybg']" :key="'tjssybg' + index">
        <tjssybg ref="tjssybg" :excel-id="item" :page="index" :syType="form.syType"></tjssybg>
      </div>

      <div v-show="activeVal === 'tdczbhsl'" v-for="(item, index) in excelTypeMap['tdczbhsl']"
        :key="'tdczbhsl' + index">
        <tdczbhsl ref="tdczbhsl" :excel-id="item" :page="index" :syType="form.syType"></tdczbhsl>
      </div>

    </div>
    <backtop-view :sum="activeVal ? excelTypeMap[activeVal].length : 1" @onModPages="onModPages" />

    <statement-select-view ref="statementSelectView" :data-list="tabList" @onShowPreview="onShowPreview" />
    <statement-select-dialog ref="statementSelectDialog" />
  </div>
</template>

<script>
import Bus from "@/utils/bus";
import store from "@/store";
import { emptyConvert, calculateMeanvalue } from "@/views/reportForms/utils";
import { getBsDataReport, bsDataReportTb, bsDataReportModPages } from "@/api/syrw/bsDataReport";
import StatementSelectView from "@/views/reportForms/components/StatementSelectView.vue";
import StatementSelectDialog from "@/views/reportForms/components/StatementSelectDialog.vue";
import decimal from "@/utils/big-decimal";

export default {
  name: "index",
  components: {
    TopOperation: () => import("@/views/reportForms/components/topOperation"), // tab页
    BacktopView: () => import("@/views/reportForms/components/backtopView"), // 回到顶部页
    tdhslb: () => import("@/views/reportForms/tg/tdhslb"), //  土的含水率
    tdmdjlb: () => import("@/views/reportForms/tg/tdmdjlb"), // 土的密度记录表
    tdjxhsljlb: () => import("@/views/reportForms/tg/tdjxhsljlb"), // 土的限界含水率记录表
    tdtrcdjlb: () => import("@/views/reportForms/tg/tdtrcdjlb"), // 土的天然稠度记录表
    tdjsjlb: () => import("@/views/reportForms/tg/tdjsjlb"), // 土的击实记录表
    tdczbjlby: () => import("@/views/reportForms/tg/tdczbjlby"), // 土的承载比(CBR)记录表一
    tdczbhsl: () => import("@/views/reportForms/tg/tdczbhsl"), // 土的承载比(CBR)含水率记录表
    tdczbjlbe: () => import("@/views/reportForms/tg/tdczbjlbe"), // 土的承载比(CBR)记录表二
    tdczbjlbs: () => import("@/views/reportForms/tg/tdczbjlbs"), // 土的承载比(CBR)记录表三
    tdsslyjzjlb: () => import("@/views/reportForms/tg/tdsslyjzjlb"), // 土的烧失量、有机质、酸碱度、易熔盐记录表
    tdklfxjlb: () => import("@/views/reportForms/tg/tdklfxjlb"), // 土的颗粒分析记录表
    tgsybg: () => import("@/views/reportForms/tg/tgsybg"), // 土工试验报告
    tjssybg: () => import("@/views/reportForms/tg/tjssybg"), // 土击实试验报告表
    StatementSelectView,
    StatementSelectDialog
  },
  data () {
    return {
      reportId: undefined,
      tabList: [],
      form: {
        syType: "",
        excelTypeList: {}
      },
      activeVal: "",
      // key为excelTypeId， value为 excelId数组
      excelTypeMap: {},
      modPagesLodding: false
    };
  },
  watch: {
    'activeVal' (val) {
      this.reportFormsSubmit(false);
    }
  },
  created () {
    if (this.$route.query) {
      this.reportId = this.$route.query.reportId;
    }
    this.init();
  },
  mounted () {
    // 填充特殊符号
    Bus.$on("setCharacter", (data) => {
      const { focusInput, page } = store.state.reportForms;
      this.$refs[this.activeVal][page].form[focusInput] = (this.$refs[this.activeVal][page].form[focusInput] || "") + data;
    });
    // 土工击实表结果有变化
    Bus.$on("Tg89ResultChange", () => {
      const zdgmdPjz = this.computeZdgmd();
      const zjhslPjz = this.computeZjhslPjz();
      // console.log("zdgmdPjz:", zdgmdPjz);
      // console.log("zjhslPjz:", zjhslPjz);
      if (zdgmdPjz) {
        Bus.$emit("setTgBgJiancjg", [
          { key: 8, value: zdgmdPjz }
        ]);
      }
      if (zjhslPjz) {
        Bus.$emit("setTgBgJiancjg", [
          { key: 9, value: zjhslPjz }
        ]);
      }
      // 给CBR含水率赋值和CBR三赋值
      Bus.$emit("setTgJszdgmdPjz", { zdgmdPjz: zdgmdPjz, zjhslPjz: zjhslPjz });
    });
  },
  beforeDestroy () {
    // 停止监听事件
    Bus.$off("setCharacter");
    Bus.$off("setTgBgJiancjg");
  },
  methods: {
    // 土工击实表结果有变化
    tg345ResultChange () {
      let yexArr = [];
      let suxArr = [];
      let suxzsArr = [];
      const refList = this.excelTypeMap["tdjxhsljlb"];
      if (!refList || refList.length <= 0) {
        return;
      }
      for (let index = 0; index < refList.length; index++) {
        if (this.$refs["tdjxhsljlb"] && this.$refs["tdjxhsljlb"][index] && this.$refs["tdjxhsljlb"][index].form) {
          const { yex, sux, suxzs } = this.$refs["tdjxhsljlb"][index].form;
          yexArr.push(yex);
          suxArr.push(sux);
          suxzsArr.push(suxzs);
        }
      }
      const yex = calculateMeanvalue(yexArr, 1);
      const sux = calculateMeanvalue(suxArr, 1);
      const suxzs = calculateMeanvalue(suxzsArr, 1);
      Bus.$emit("setTgBgJiancjg", [
        { key: "3", value: yex },
        { key: "4", value: sux },
        { key: "5", value: suxzs }
      ]);
      // 给天然稠度
      Bus.$emit("setTgBgYex", { yex: yex, sux: sux, suxzs: suxzs });

    },
    // 计算最大干密度平均值
    computeZdgmd () {
      const refList = this.excelTypeMap["tdjsjlb"];
      if (!refList || refList.length <= 0) {
        return undefined;
      }
      let zuidgmdSum = 0;
      let length = 0;
      refList.forEach((element, index) => {
        if (this.$refs["tdjsjlb"] && this.$refs["tdjsjlb"][index] && this.$refs["tdjsjlb"][index].form.zuidgmd) {
          zuidgmdSum = decimal.add(zuidgmdSum, this.$refs["tdjsjlb"][index].form.zuidgmd);
          // zuidgmdSum += this.$refs["tdjsjlb"][index].form.zuidgmd;
          length += 1;
        }
      });
      return decimal.divide(zuidgmdSum, length, 3);
    },
    // 计算最佳含水率平均值
    computeZjhslPjz () {
      const refList = this.excelTypeMap["tdjsjlb"];
      if (!refList || refList.length <= 0) {
        return undefined;
      }
      let zuijhslSum = 0;
      let length = 0;
      refList.forEach((element, index) => {
        if (this.$refs["tdjsjlb"] && this.$refs["tdjsjlb"][index] && this.$refs["tdjsjlb"][index].form.zuijhsl) {
          zuijhslSum = decimal.add(zuijhslSum, this.$refs["tdjsjlb"][index].form.zuijhsl);

          // zuijhslSum += this.$refs["tdjsjlb"][index].form.zuijhsl;
          length += 1;
        }
      });
      return decimal.divide(zuijhslSum, length, 1);
    },
    // 获取记录表和报告表
    async init () {
      if (!this.reportId) {
        this.$message.error("报告id不能为空");
        return;
      }
      const response = await getBsDataReport(this.reportId);
      this.form = response.data;
      if (!this.form.excelTypeList || this.form.excelTypeList.length == 0) {
        this.tabList = [
          {
            val: undefined,
            name: undefined
          }
        ];
        return;
      }
      let tabList = [];
      this.excelTypeMap = {};
      this.form.excelTypeList.forEach((item) => {
        // 是否包含label
        if (this.excelTypeMap.hasOwnProperty(item.label)) {
          this.excelTypeMap[item.label].push(item.excelId);
        } else {
          this.excelTypeMap[item.label] = [item.excelId];
          tabList.push({
            val: item.label,
            name: item.excelTypeName
          });
        }
      });
      this.tabList = tabList;
      this.$set(this.tabList, tabList);
      if (!this.activeVal) {
        this.activeVal = this.tabList[0].val;
      }
      // this.currentActive(this.activeVal)
    },
    currentActive (val) {
      this.activeVal = val;
    },
    // 批量提交
    async reportFormsSubmit (hasTips = true) {
      let arr = [];
      this.tabList.forEach((item) => {
        const refList = this.excelTypeMap[item.val];
        if (!refList || refList.length <= 0) {
          return;
        }
        refList.forEach((element, index) => {
          if (this.$refs[item.val] && this.$refs[item.val][index]) {
            let info = this.$refs[item.val][index].submitForm();
            arr.push({ label: item.val, excelObj: emptyConvert(JSON.parse(JSON.stringify(info))) });
          }
        });
      });
      await bsDataReportTb(arr);
      if (hasTips) {
        this.$modal.msgSuccess("操作成功");
      }
    },
    // 加一页减一页
    async onModPages (modType) {
      this.modPagesLodding = true;
      // 1先保存
      await this.reportFormsSubmit();
      // 2 加减页
      await this.doModPages(modType);
      // 3 重新加载
      await this.init();
      // 4 刷新
      this.tabList.forEach((item) => {
        const refList = this.excelTypeMap[item.val];
        if (!refList || refList.length <= 0) {
          return;
        }
        refList.forEach((element, index) => {
          if (this.$refs[item.val] && this.$refs[item.val][index]) {
            this.$refs[item.val][index].getInfo();
          }
        });
      });
      this.modPagesLodding = false;
    },
    // 加减页
    async doModPages (modType) {
      const arr = this.excelTypeMap[this.activeVal];
      const data = {
        reportId: this.reportId,
        label: this.activeVal,
        modType: modType,
        excelId: Number(modType) < 0 ? arr[arr.length - 1] : undefined,
        vo: Number(modType) < 0 ? undefined : this.$refs[this.activeVal][arr.length - 1].submitForm()
      };
      await bsDataReportModPages(data);
    },
    // 选择报表
    onPrintSpecificPart () {
      this.$refs.statementSelectView.init();
    },
    //预览
    onShowPreview (tableList) {
      if (!tableList || tableList.length <= 0) {
        return;
      }
      let arr = [];
      this.form.excelTypeList.forEach((item) => {
        if (tableList.includes(item.label)) {
          arr.push({ label: item.label, excelId: item.excelId, syType: this.form.syType, typeSort: item.excelIndex });
        }
      });
      this.$refs.statementSelectDialog.init(arr);
    }
  }
};
</script>

<style scoped></style>
